package com.eric.windows.imgDispose;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import org.androidannotations.annotations.EView;

/**
 * <h3>Description</h3>
 * TODO
 * <h3>Author</h3> Eric
 * <h3>Date</h3> 2017/8/14 15:30
 * <h3>Copyright</h3> Copyright (c)2017 Shenzhen TLIT  Co., Ltd. Inc. All rights reserved.
 */

@EView
public class ShapeView extends View {

    public ShapeView(Context context) {
        super(context);
    }

    public ShapeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ShapeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ShapeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        //反锯齿
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        //绘制圆型 需要确定 弧心坐标和半径 如（40，40） 半径为40
        canvas.drawCircle(40,40,30,paint);
        //绘制矩形 需要确定两个坐标 即left top （10,80） 与 right bottom （70,140）
        canvas.drawRect(10,80,70,140,paint);
        // 需要确定两个坐标 即left top （10,150） 与 right bottom （70,230）
        RectF  rectF  = new RectF(10,150,70,230);
        //绘制圆角矩形   rx,  ry ??
        canvas.drawRoundRect(rectF,15,15,paint);

        RectF  rectF2  = new RectF(10,240,70,270);
        //绘制椭圆
        canvas.drawOval(rectF2,paint);
        //绘制三角形
        Path  path  = new Path();
        //设置起始点
        path.moveTo(10,290);
        path.lineTo(50,340);
        path.lineTo(80,290);
        path.close();
        canvas.drawPath(path,paint);

        //绘制一个五角形
        Path  path2  = new Path();
        //设置起始点
        path2.moveTo(26,360);
        path2.lineTo(54,360);
        path2.lineTo(70,390);
        path2.lineTo(40,420);
        path2.lineTo(10,390);
        path2.close();
        canvas.drawPath(path2,paint);

        //===========设置画笔填充风格后绘制===========
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);
        canvas.drawCircle(120,40,30,paint);
        canvas.drawRect(90,80,150,140,paint);

        //绘制圆角矩形   rx,  ry ??
        canvas.drawRoundRect(90,150,150,230,15,15,paint);
        canvas.drawOval(90,240,150,270,paint);

        //绘制三角形
        Path  path3  = new Path();
        //设置起始点
        path3.moveTo(90,290);
        path3.lineTo(140,340);
        path3.lineTo(160,290);
        path3.close();
        canvas.drawPath(path3,paint);

        //绘制一个五角形
        Path  path4  = new Path();
        //设置起始点
        path4.moveTo(116,360);
        path4.lineTo(154,360);
        path4.lineTo(170,390);
        path4.lineTo(130,420);
        path4.lineTo(90,390);
        path4.close();
        canvas.drawPath(path4,paint);

        //===========设置渐变器后绘制===========
        //渐变器
        @SuppressLint("DrawAllocation")
        Shader  shader = new LinearGradient(0,0,40,60,new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},null, Shader.TileMode.REPEAT);
        paint.setShader(shader);
        //设置阴影
        paint.setShadowLayer(45,10,10,Color.GRAY);
        canvas.drawCircle(210,40,30,paint);
        canvas.drawRect(180,80,240,140,paint);
//        canvas.drawRoundRect(90,150,150,230,15,15,paint);
        canvas.drawRoundRect(180,150,240,230,15,15,paint);
        canvas.drawOval(180,240,240,270,paint);
        //绘制三角形
        Path  path5  = new Path();
        //设置起始点
        path5.moveTo(180,290);
        path5.lineTo(230,340);
        path5.lineTo(250,290);
        path5.close();
        canvas.drawPath(path5,paint);

        //绘制一个五角形
        Path  path6  = new Path();
        //设置起始点
        path6.moveTo(206,360);
        path6.lineTo(244,360);
        path6.lineTo(260,390);
        path6.lineTo(220,420);
        path6.lineTo(180,390);
        path6.close();
        canvas.drawPath(path6,paint);
        //=============绘制文字=============
        Paint  p = new Paint();
        p.setTextSize(24);
        canvas.drawText("圆形",270,40,p);
        canvas.drawText("矩形",270,115,p);
        canvas.drawText("圆角矩形",270,210,p);
        canvas.drawText("椭圆",270,250,p);
        canvas.drawText("三角形",270,320,p);
        canvas.drawText("五角形",270,370,p);

    }


}
